﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class admin_Upload_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        ShowFolder();
        ShowFile();
        GetBackLink();
      }
    }
    private void GetBackLink()
    {
      if (Request.QueryString["folder"] == null) linkBack.Enabled = false;
      else
      {
        string folder = "Upload-" + Request.QueryString["folder"];
        string url = "";
        if (folder.Contains("-"))
        {
          int index = folder.LastIndexOf("-");          
          url = "?folder=" + folder.Substring(0, index).Replace("Upload-", "");
          if (url == "?folder=Upload") url = "";
        }
        linkBack.NavigateUrl = "List.aspx" + url;
      }
    }
    public string SubStringFolder(string str)
    {
      int index = str.LastIndexOf(@"\");
      return str.Substring(index + 1, str.Length - index-1);
    }
    private void ShowFolder()
    {
      ulFolder.Controls.Clear();
      string folder = "Upload";      
      if (Request.QueryString["folder"] != null)
      {
        folder = "Upload/" + Request.QueryString["folder"].Replace("-", "/");        
      }
      string[] listFolder = Directory.GetDirectories(MyConfig.serverPath + folder);
      foreach (string item in listFolder)
      {
        HtmlGenericControl li = new HtmlGenericControl("li");
        string folderName = SubStringFolder(item);      

        HyperLink link = new HyperLink();
        link.NavigateUrl = "List.aspx?folder=" + (Request.QueryString["folder"] != null ? Request.QueryString["folder"] + "-" : "") + folderName;

        HtmlGenericControl span = new HtmlGenericControl("span");
        span.InnerText = folderName;
        link.Controls.Add(span);

        li.Controls.Add(link);

        ulFolder.Controls.Add(li);        
      }
    }
    private void ShowFile()
    {
      string folder = "Upload";
      if (Request.QueryString["folder"] != null)
      {
        folder = "Upload/" + Request.QueryString["folder"].Replace("-", "/");        
      }
      string[] files = Directory.GetFiles(MyConfig.serverPath + folder);
      foreach (var item in files)
      {
        HtmlGenericControl li = new HtmlGenericControl("li");
        string fileName = Path.GetFileName(item);
        string extension = Path.GetExtension(item).ToUpper();

        HyperLink link = new HyperLink();
        link.Target = "new";
        link.NavigateUrl = "../../" + folder + "/" + fileName;

        HtmlGenericControl small = new HtmlGenericControl("small");
        small.InnerText = (fileName.Length > 10 ? fileName.Substring(0, 10) + "..." : fileName);

        Image image = new Image();
        image.Height = 50; image.Width = 50;
        image.Attributes["title"] = fileName;

        if (extension == ".JPG" || extension == ".GIF" || extension == ".PNG" || extension == ".JPEG" || extension == ".ICO")
        {
          image.ImageUrl = "../../" + folder + "/" + fileName;
        }
        else if (extension == ".PDF")
        {
          image.ImageUrl = "../../Images/pdf-icon.gif";
        }
        else if (extension == ".DOC")
        {
          image.ImageUrl = "../../Images/doc-icon.gif";
        }
        else if (extension == ".PSD")
        {
          image.ImageUrl = "../../Images/psd-icon.jpg";
        }
        else if (extension == ".RAR")
        {
          image.ImageUrl = "../../Images/rar-icon.gif";
        }
        else if (extension == ".FLV" || extension == ".AVI" || extension == ".MP4" || extension == ".MPEG")
        {
          image.ImageUrl = "../../Images/video-icon.gif";
        }
        else
        {
          image.ImageUrl = "../../Images/file-icon.png";
        }

        link.Controls.Add(image);
        link.Controls.Add(small);

        li.Controls.Add(link);

        ulFile.Controls.Add(li);   
      }
    }
    private void Enable(bool enable)
    {
      tblCreate.Visible = enable;
      btnAdd.Visible = !enable;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      Enable(true);
      ShowFolder();
      ShowFile();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      Enable(false);
      ShowFolder();
      ShowFile();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        ShowFolder();
        ShowFile();
        return;
      }
      try
      {
        lbThongBao.Text = "";
        string folder = "Upload/";
        if (Request.QueryString["folder"] != null) folder = "Upload/" + Request.QueryString["folder"].Replace("-", "/") + "/";     
        Directory.CreateDirectory(MyConfig.serverPath + folder + txtName.Text.Trim().Replace("-"," "));
        lbThongBao.Text = "Thư mục " + txtName.Text + " đã được tạo !";
        Enable(false);
        ShowFolder();
        ShowFile();
      }
      catch(Exception ex) {
        lbThongBao.Text =  ex.Message;
      }
    }
    private void Enable_Upload(bool isVisible)
    {
      lbUpload.Visible = isVisible;
      FilePicker1.Visible = isVisible;
      linkUpload.Visible = !isVisible;
      btnUploadCancel.Visible = isVisible;
      btnRefresh.Visible = isVisible;
    }
    protected void linkUpload_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        ShowFolder();
        ShowFile();
        return;
      }
      lbThongBao.Text = "";
      Enable_Upload(true);
      ShowFolder();
      ShowFile();

      string folder = "Upload/";
      if (Request.QueryString["folder"] != null) folder = "Upload/" + Request.QueryString["folder"].Replace("-", "/") + "/";
      FilePicker1.fpUploadDir = "../" + folder;
    }
    protected void btnUploadCancel_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        return;
      }
      lbThongBao.Text = "";
      Enable_Upload(false);
      ShowFolder();
      ShowFile();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      ShowFolder();
      ShowFile();
    }
}